var KOC = require("koc.common.utils");

var Config = require("../../config");

var StateData = require("../statedata");
var DALFactory = require("../../lib/dal/dal-factory");
var CommonUtils = require("../../lib/common-utils");
var Interface_heepay = require("../../lib/interface-heepay");
moment = require('moment-timezone');

module.exports = {
    Service: function (req, res) {
        var _UserData = StateData.Value(req, res);//取得状态
        switch (req.params.type) {
            //请求地址
            case "business.user-payorder":
                HeepayFactory.Business.User_PayOrder(req, res, _UserData);
                return;
            case "business.user-payoney":
                HeepayFactory.Business.User_PayMoney(req, res, _UserData);
                return;
            case "business.user-paymobile":
                HeepayFactory.Business.User_PayMobile(req, res, _UserData);
                return;
            //end
        }
        res.jsonp({flag: -9999, msg: "未知的请求"});
    }
};

var HeepayFactory = {
    Business: {
        User_PayOrder: function (req, res, _UserData) {
            if (!_UserData.IsLogin) {
                res.jsonp({flag: -1, msg: "您未登录"});
                return;
            }
            var _parm = {
                User_Id: _UserData.User_ID,
                pay_type: req.params.pay_type,
                pay_amt: req.params.pay_amt,
                pay_code: req.params.pay_code,
                is_phone: KOC.StringUtils.ToIntPositive(req.params.is_phone),
                is_frame: KOC.StringUtils.ToIntPositive(req.params.is_frame),
                Pay_From: req.params.Pay_From,
                IP: KOC.StringUtils.GetClientIP(req)
            };
            if (!_parm.pay_type) {
                res.jsonp({flag: 1, msg: "pay_type:请选择支付类型"});
                return;
            }
            if (!_parm.pay_amt) {
                res.jsonp({flag: 2, msg: "请输入支付金额或输入支付金额"});
                return;
            }
            if (_parm.is_frame && !KOC.Lodash.includes([0, 1], _parm.is_frame)) {
                res.jsonp({flag: 3, msg: "is_frame:传输参数出错"});
                return;
            }
            if (_parm.is_phone && !KOC.Lodash.includes([0, 1], _parm.is_phone)) {
                res.jsonp({flag: 4, msg: "is_phone:传输参数出错"});
                return;
            }
            //插入支付数据
            DALFactory.Business.User_PayOrder({
                User_ID: _parm.User_Id,
                Pay_Type: _parm.pay_type,
                Pay_Style: _parm.pay_code,
                Pay_From: _parm.Pay_From ? _parm.Parm : 0,//0 网站 1 android 2 ios,3,微信公共号支付'
                Pay_Money: _parm.pay_amt,
                Create_IP: KOC.StringUtils.GetClientIP(req)
            }, function (retValue) {
                if (retValue.hasError) {
                    res.jsonp({flag: retValue.errorCode, msg: retValue.message});
                    return;
                }
                var _PayData = {
                    pay_type: retValue.returnObject.Pay_Type,
                    pay_amt: retValue.returnObject.Pay_Money,
                    pay_code: retValue.returnObject.Pay_Style,
                    ordercode: retValue.returnObject.Order_No,
                    is_phone: _parm.is_phone,
                    is_frame: _parm.is_frame,
                    IP: retValue.returnObject.Create_IP
                };
                res.redirect(Interface_heepay.Send(_PayData));
            });
        },
        User_PayMoney: function (req, res, _UserData) {
            var _parm = {
                "result": req.params.result,
                "pay_message": req.params.pay_message,
                "agent_id": req.params.agent_id,
                "jnet_bill_no": req.params.jnet_bill_no,
                "agent_bill_id": req.params.agent_bill_id,
                "pay_type": req.params.pay_type,
                "pay_amt": req.params.pay_amt,
                "remark": req.params.remark,
                "sign": req.params.sign
            };
            _parm.agentId = Config.Interface.Heepay.WEB.AgentID;
            _parm.key = Config.Interface.Heepay.WEB.Key;
            if (_parm.result != 1) {
                res.jsonp({flag: 1, msg: _parm.pay_message});
                return;
            }
            if (_parm.pay_amt == 0.00) {
                res.jsonp({flag: 1, msg: "支付失败，请重新支付"});
                return;
            }
            var _signString = KOC.StringUtils.MD5(
                "result=" + _parm.result +
                "&agent_id=" + _parm.agentId +
                "&jnet_bill_no=" + _parm.jnet_bill_no +
                "&agent_bill_id=" + _parm.agent_bill_id +
                "&pay_type=" + _parm.pay_type +
                "&pay_amt=" + _parm.pay_amt +
                "&remark=" + _parm.remark +
                "&key=" + _parm.key
            );
            if (_parm.sign != _signString) {
                res.jsonp({flag: 1, msg: "支付失败！请重新支付"});
                return;
            }
            //插入支付数据
            DALFactory.Business.User_PayMoney({
                Pay_Money: _parm.pay_amt,
                Order_No: _parm.agent_bill_id,
                Pay_TransNumber: _parm.jnet_bill_no,
                Create_IP: KOC.StringUtils.GetClientIP(req)
            }, function (retValue) {
                if (retValue.hasError) {
                    res.write("error");
                    res.end();
                    return;
                }
                res.write("ok");
                res.end();
            });
        },
        User_PayMobile: function (req, res, _UserData) {
            var _parm = {
                "result": req.params.result,
                "pay_message": req.params.pay_message,
                "agent_id": req.params.agent_id,
                "jnet_bill_no": req.params.jnet_bill_no,
                "agent_bill_id": req.params.agent_bill_id,
                "pay_type": req.params.pay_type,
                "pay_amt": req.params.pay_amt,
                "remark": req.params.remark,
                "sign": req.params.sign
            };
            if (_parm.result != 1) {
                res.jsonp({flag: 1, msg: _parm.pay_message});
                return;
            }
            //移动端赋值
            _parm.agentId = Config.Interface.Heepay.APP.AgentID;
            if (_parm.pay_amt == 0.00) {
                res.jsonp({flag: 1, msg: "支付失败，请重新支付"});
                return;
            }
            var _signString = KOC.StringUtils.MD5(
                "result=" + _parm.result +
                "&agent_id=" + _parm.agentId +
                "&jnet_bill_no=" + _parm.jnet_bill_no +
                "&agent_bill_id=" + _parm.agent_bill_id +
                "&pay_type=" + _parm.pay_type +
                "&pay_amt=" + _parm.pay_amt +
                "&remark=" + _parm.remark +
                "&key=" + Config.Interface.Heepay.APP.Key
            );
            if (_parm.sign != _signString) {
                res.jsonp({flag: 1, msg: "支付失败！请重新支付"});
                return;
            }
            //插入支付数据
            DALFactory.Business.User_PayMoney({
                Pay_Money: _parm.pay_amt,
                Order_No: _parm.agent_bill_id,
                Pay_TransNumber: _parm.jnet_bill_no,
                Create_IP: KOC.StringUtils.GetClientIP(req)
            }, function (retValue) {
                if (retValue.hasError) {
                    res.write("error");
                    res.end();
                    return;
                }
                res.write("ok");
                res.end();
            });
        }
    }
}